/*
 * Copyright 2007 National Bioinformatics Network
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package dgbgui.tools;

import java.awt.event.ActionEvent;
import java.util.HashSet;
import java.util.Set;

import dgbgui.DGBGUI;
import dgbgui.command.CommandStack;
import dgbgui.command.ReplaceQueryCommand;
import dgbgui.gui.dialogs.EditInstanceDialog;
import dgbgui.modeldocument.AttributePrototype;
import dgbgui.modeldocument.ModelDocument;
import dgbgui.modeldocument.Query;
import dgbgui.modeldocument.definition.FilteredInstanceDefinition;
import dgbgui.selection.SelectionManager;

public class EditConceptronInstancePrototypeTool extends DialogedTool {

    public void execute() {

        // Get the prototype to node to edit
        // Only one protoype node can be selected fo this tool
        Set<Object> selectedNodes = SelectionManager.getInstance().getSelection(Query.class);
        assert ( selectedNodes.size() == 1 );
        m_nodeToEdit = (Query)selectedNodes.iterator().next();
        assert ( m_nodeToEdit.getDefinition() instanceof FilteredInstanceDefinition );
        m_definition = (FilteredInstanceDefinition)m_nodeToEdit.getDefinition();

        // Create the m_dialog for editing the attributes. 
        // the true parameter indicates that the m_dialog should be modal
        m_dialog = new EditInstanceDialog(this,m_definition);
        m_dialog.pack();
        DGBGUI.centerDialog(m_dialog);
        m_dialog.setVisible(true);


    }
    

    public void actionPerformed(ActionEvent event) {
        
        if ( event.getSource() == m_dialog.getOkButton() ) {
            // Create a new node
            FilteredInstanceDefinition newDef = (FilteredInstanceDefinition)m_definition.clone();
            
            // Create the new list of attribute prototypes
            Set<String> attribNames = ModelDocument.getInstance().getAttributeNames(m_definition.getConceptronType());
            Set<AttributePrototype> newAttribProtos = new HashSet<AttributePrototype>(); 
            for ( String name : attribNames ) {
                String filterString = ((EditInstanceDialog)m_dialog).getFilterString(name);
                if ( !filterString.equals("") ) {
                    AttributePrototype.FilterType filterType = ((EditInstanceDialog)m_dialog).getFilterType(name);
                    newAttribProtos.add( new AttributePrototype(name,filterType,filterString));
                }
            }
            newDef.setAttribProtoTypes(newAttribProtos);
            Query newNode = new Query(newDef);
            
            CommandStack.getInstance().execute(new ReplaceQueryCommand(m_nodeToEdit,newNode));

        }
        
        // Close the tool
        doClose();

    }

    Query m_nodeToEdit;
    FilteredInstanceDefinition m_definition;

}
